Thread: help using || (or) in if statement (working code)

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    Question help using || (or) in if statement (working code)

    hey guys this is probably an easy fix but i was wounderingif any1 has answers for why
    Code:
     printf("\nThere is no employee with that I.D. number\n");
    always prints even when its not meant to

    heres where it is happening
    Code:
    void employees_index_search(struct employees employee[], int n)
    {
        int indexsearch, i;
        printf("Enter the employee I.D. for who you would like to see information on:\n");
        scanf(" %d", &indexsearch);
        for(i=0;i<n;i++)
        {
            if(indexsearch == employee[i].EmpId)
            {
                printf("\n%d\t\t%s\t\t$%.2f\t\t", employee[i].EmpId, employee[i].Name, employee[i].Salary);
            }
        }
        if(indexsearch != employee[0].EmpId || employee[1].EmpId || employee[2].EmpId)
        {
            printf("\nThere is no employee with that I.D. number\n");
        }
    }
    and here is my full code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct employees {
        int EmpId;
        char Name[25];
        float Salary;
    }s[3];
    
    
    void read_employees(struct employees employee[], int n)
    {
        int i;
        for(i=0;i<n;i++)
        {
            printf("Enter employee %d's ID:\n", i+1);
            scanf("%d", &employee[i].EmpId);
            printf("Enter employee %d's name: \n", i+1);
            scanf(" %s", employee[i].Name);
            printf("Enter employee %d's salary:\n$", i+1);
            scanf(" %f", &employee[i].Salary);
    
    
        }
        printf("\n\n");
    }
    
    
    void print_employees(struct employees employee[], int n)
    {
        int i;
        printf("\t\t---printing data---\n");
        for (i=0;i<n;i++)
        {
            printf("%d\t\t%s\t\t$%.2f\t\t", employee[i].EmpId, employee[i].Name, employee[i].Salary);
            if(employee[i].Salary <4000)
            {
                printf("Level A");
            }
            if(employee[i].Salary >5000)
            {
                printf("Level B");
            }
            printf("\n");
        }
    }
    
    
    void employees_total_salary(struct employees employee[], int n, float *total)
    {
        int i;
    
    
        for(i=0;i<n;i++)
        {
            *total = *total + employee[i].Salary;
        }
    }
    
    
    void employees_index_search(struct employees employee[], int n)
    {
        int indexsearch, i;
        printf("Enter the employee I.D. for who you would like to see information on:\n");
        scanf(" %d", &indexsearch);
        for(i=0;i<n;i++)
        {
            if(indexsearch == employee[i].EmpId)
            {
                printf("\n%d\t\t%s\t\t$%.2f\t\t", employee[i].EmpId, employee[i].Name, employee[i].Salary);
            }
        }
        if(indexsearch != employee[0].EmpId || employee[1].EmpId || employee[2].EmpId)
        {
            printf("\nThere is no employee with that I.D. number\n");
        }
    }
    
    
    int main()
    {
        int n =3;
        float total;
    
    
        read_employees(s, n);
        print_employees(s, n);
        employees_total_salary(s, n, &total);
        printf("\ntotal salary = $%.2f\n", total);
        employees_index_search(s, n);
    
    
        system("pause");
        return 0;
    }

    thankyou, also if you have any other advice it would be well recieved

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OR is usually used to combine comparisons, meaning to say indexsearch is not this, that or the other thing, you have to individually compare each.

    indexsearch != employee[0].EmpId || indexsearch != employee[1].EmpId || indexsearch != employee[2].EmpId

    Furthermore, it would be better to use AND, now that I get a closer look at it. You want to print something when != is true for all comparisons, not any of them (as with OR).
    Last edited by whiteflags; 12-18-2017 at 06:26 PM.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    i see xD thanks heaps and i agree i should change it to &&

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    You can get rid ot the second if statement.
    Code:
    void employees_index_search(struct employees employee[], int n)
    {
      int indexsearch, i;
      printf("Enter the employee I.D. for who you would like to see 
               information on:\n");
      scanf(" %d", &indexsearch);
      for (i = 0; i<n; i++)
      {
        if (indexsearch == employee[i].EmpId)
        {
          printf("\n%d\t\t%s\t\t$%.2f\t\t", employee[i].EmpId,
                   employee[i].Name, employee[i].Salary);
          return;
        }
      }
      /* only gets here if not found */
      printf("\nThere is no employee with that I.D. number\n");
    }

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    oh i like that even more thnx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. If statement not working
    By howardbc14 in forum C Programming
    Replies: 1
    Last Post: 05-07-2015, 07:51 PM
  2. If statement not working
    By BIGDENIRO in forum C Programming
    Replies: 1
    Last Post: 10-10-2013, 02:58 PM
  3. The following if statement is not working!...
    By darkmagic in forum C Programming
    Replies: 5
    Last Post: 06-23-2010, 08:11 AM
  4. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM
  5. If statement not working!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2002, 02:45 AM

Tags for this Thread